package com.java.tread;

import java.util.Random;

public class ThreadLocalTest {


	public static void main(String[] args) {
		for (int i = 0; i < 2; i++) {
//			new Thread(){
//				@Override
//				public void run() {
//					int data=new Random().nextInt();
//					Mydata.getInstance().setData(data);
//					System.out.println(Thread.currentThread().getName()+"has put data:"+data);
//					System.out.println(Thread.currentThread().getName()+"has get data:A"+new A().get());
//					System.out.println(Thread.currentThread().getName()+"has get data:B"+new B().get());
//					
//				}
//			}.start();
			int date=2<<35;
			System.out.println(date);
			
		}
	}
	
	static class A{
		public int get(){
			try {
				Thread.sleep(new Random().nextInt(1000));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return Mydata.getInstance().getData();
		}
	}
	
	static class B{
		public int get(){
			try {
				Thread.sleep(new Random().nextInt(1000));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return Mydata.getInstance().getData();
		}
	}
	
}

class Mydata{
	private static ThreadLocal instanceHodler = new ThreadLocal();
	private int data;
	public int getData() {
		return data;
	}
	public void setData(int data) {
		this.data = data;
	}
	public Mydata() {
	}
	public static Mydata getInstance(){
		Mydata instance=(Mydata)instanceHodler.get();
		if(instance==null){
			instance=new Mydata();
			instanceHodler.set(instance);
		}
		return instance;
	}
}